package cn.player.download.file;


import java.io.File;
import java.io.IOException;

/**
 * @author Alexey Danilov (danikula@gmail.com).
 */
public class FileCache {

    private static final String TEMP_POSTFIX = ".download";

    private DiskUsage diskUsage = null;
    public File file;

    public FileCache(File file) {
        this(file, new UnlimitedDiskUsage());
    }

    public FileCache(File file, DiskUsage diskUsage) {
        try {
            if (diskUsage == null) {
                throw new NullPointerException();
            }
            this.diskUsage = diskUsage;
            File directory = file.getParentFile();
            Files.makeDir(directory);
            this.file = file;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void close() {
        try {
            boolean completed = file.exists();
            this.file = completed ? file : new File(file.getParentFile(), file.getName());
            diskUsage.touch(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
